package com.liseen.resource
{
	import com.liseen.events.CgLoaderEvent;
	import com.liseen.events.ResLoaderEvent;
	import com.liseen.loader.CgLoader;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.ProgressEvent;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;

	/**
	 * 资源加载队列
	 * @author Liseen
	 * 
	 */	
	public class ResLoaderList extends EventDispatcher
	{
		
		
		private var _loadList:Array;
		private var _loadedNum:int;
		private var _totalNum:int;
		private var _currentLoadItem:ResLoaderItem;
		private var _isLoading:Boolean;
		public function ResLoaderList()
		{
			_loadList = [];
			CgLoader.getInstance().addEventListener(CgLoaderEvent.CG_ITEM_LOADED,onItemLoadCompleteHandle);
			CgLoader.getInstance().addEventListener(CgLoaderEvent.CG_ITEM_LOADING,onItemLoadPrgressHandle);
			CgLoader.getInstance().addEventListener(CgLoaderEvent.CG_ITEM_LOADERROR,onItemLoadErrorHandle);
		}
		
		
		/**
		 *获取加载队列长度 
		 * @return 
		 * 
		 */		
		public function get totalNum():int
		{
			return this._totalNum;
		}
		/**
		 *获取当前加载完的个数 
		 * @return 
		 * 
		 */		
		public function get loadedNum():int
		{
			return this._loadedNum;
		}
		
		/**
		 *添加加载单元 
		 * @param item
		 * 
		 */		
		public function addItem(item:ResLoaderItem):void
		{
			this._loadList.push(item);
			this._totalNum++;
		}
		/**
		 *开始加载 
		 * 
		 */		
		public function load():void
		{
			if(_loadList.length==0)
			{
				this.dispatchEvent(new ResLoaderEvent(ResLoaderEvent.ALL_COMPLETE));
				this._isLoading=false;
				return;
			}
			
			if(this._isLoading==false)
			{
				this._isLoading = true;
				this._currentLoadItem = _loadList.shift() as ResLoaderItem;
				
				CgLoader.getInstance().loadFile(_currentLoadItem.url,_currentLoadItem.preventCache);
				
				this.dispatchEvent(new ResLoaderEvent(ResLoaderEvent.ITEM_START));
			}
			
		}
		
		private function onItemLoadCompleteHandle(event:CgLoaderEvent):void
		{	
			if(this._currentLoadItem==null)
			{
				return;
			}
			
			if(event.loadItem.url == this._currentLoadItem.url)
			{
				this._loadedNum++;
				this._currentLoadItem.data = event.loadItem.content;
				this.dispatchEvent(new ResLoaderEvent(ResLoaderEvent.ITEM_COMPLETE,this._currentLoadItem,false,false));
				this._currentLoadItem=null;
				this._isLoading=false;
				this.load();
			}
			
		}
		
		private function onItemLoadPrgressHandle(event:CgLoaderEvent):void
		{
			if(this._currentLoadItem==null)
			{
				return;
			}
			
			if(event.loadItem.url == this._currentLoadItem.url)
			{
				this._currentLoadItem.byteLoaded = event.loadItem.bytesLoaded;
				this._currentLoadItem.byteTotal = event.loadItem.bytesTotal;
				this.dispatchEvent(new ResLoaderEvent(ResLoaderEvent.PROGRESS,this._currentLoadItem,false,false));
			}
		}
		
		private function onItemLoadErrorHandle(event:CgLoaderEvent):void
		{
				
		}
	}
}